var gulp = require('gulp');
var rename = require('gulp-rename'); //文件重命名
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var vinylPaths = require('vinyl-paths');
var del = require("del");
var replace = require('gulp-string-replace');
var rev = require('gulp-rev');
var revCollector = require("gulp-rev-collector");
var clean = require('gulp-clean');
var minimist = require('minimist');
var zip = require('gulp-zip');
var upload = require('gulp-file-post');
var json={};


var knownOptions = {
	string: 'env',
	default: { env: process.env.NODE_ENV || 'production' }
};
var webName = minimist(process.argv.slice(2), knownOptions).env;

if(!webName){
	console.log('error: Not path!');
	return;
}
if(webName[webName.length-1]!='/'){
	webName=webName+'/';
}

// gulp --env 11satellite_demo.com
var webUrl = '';

var webDev = webUrl+webName; //网站开发目录 || '../satellite_demo.com/'


var webDevJsPath = webDev + 'assets/js/'; //js开发路径;
var webDevCssPath = webDev + 'assets/css/'; //css开发路径;
var webDevImagesPath = webDev + 'assets/images/'; //images开发路径;


var webDist = webDev+'dist'+new Date().getTime()+'/'; //网站输出目录 || '../webdist/'


var zipName = new Date().getTime()+'.zip'; //zip文件名
var assetsDist = webDist + 'assets/'; //网站静态资源目录
var tmpFile = webDist+'tmp' //存放的临时中转文件
var tmpFileJs = webDist+'tmp/js' //临时js文件
var tmpFileCss = webDist+'tmp/css' //临时css文件
var tmpFileImages = webDist+'tmp/images' //临时images文件

//参数传递执行命令： gulp --env 


//清除旧的网站
gulp.task('clean',function() {
	return gulp.src(webDist,{read: false})
		.pipe(clean({force: true}));
});

//克隆整个网址
gulp.task('copy',['clean'],function() {
	return gulp.src(webDev + '**/*')
		.pipe(gulp.dest(webDist));
});


//清除资源文件
gulp.task('cleanAssets',['copy'],function() {
	return gulp.src(webDist+'assets',{read: false})
		.pipe(clean({force: true}));
});


//压缩js
gulp.task('jscompress', ['cleanAssets'], function() {
	return gulp.src(webDevJsPath + '**/*.js')
		.pipe(uglify())
		.pipe(gulp.dest(tmpFileJs))
});

//压缩css
gulp.task('cssmini',['jscompress'], function() {
	return gulp.src(webDevCssPath + '**/*.css')
		.pipe(minify())
		.pipe(gulp.dest(tmpFileCss))
});

//克隆images图片
gulp.task('copyImages',['cssmini'],function() {
	return gulp.src(webDevImagesPath + '**/*')
		.pipe(gulp.dest(tmpFileImages));
});

//生成hash压缩与创建json
gulp.task('dev',['copyImages'], function() {
	return gulp.src([tmpFileJs + "**/*.js",tmpFileCss + "**/*.css",tmpFileImages + "**/*"])
		.pipe(rev()) //生成hash压缩css与js
		.pipe(gulp.dest(assetsDist))//输出至目录
        .pipe(rev.manifest()) //生成json文件
		.pipe(gulp.dest(tmpFile))//输出json文件
}); 

//加版本号
gulp.task('revHtml',['dev'], function () {
    return gulp.src([webDist+'**/*.json', webDist+'**/*.html']) //查询哪个json,替换哪些模版
        .pipe(revCollector({  //开始替换
            replaceReved:true
        }))
        .pipe(gulp.dest(webDist)); //输出替换
});

//清除临时中转文件tmpFile
gulp.task('cleanTmpFile0',['revHtml'],function() {
	return gulp.src([tmpFile],{read: false})
		.pipe(clean({force: true}));
});

//打包生成zip
gulp.task('zip',['cleanTmpFile0'],function() {
    return gulp.src(webDist+'**/*')
        .pipe(zip(zipName))
        .pipe(gulp.dest(webDist));
});
//清除临时中转文件tmpFile
/*gulp.task('cleanTmpFile1',['zip'],function() {
	return gulp.src([webDist],{read: false})
		.pipe(clean({force: true}));
});*/


// upload a zip file and decompress on the server
gulp.task('upload',['zip'], function() {

	console.log('send!')
	console.log('upload '+webDist+zipName);
	console.log('destdir '+webDist);
	return gulp.src(webDist+zipName)
		.pipe(upload({
			url: 'http://10.0.0.4:9875/upload?destdir='+webDist,
			callback: function(res) {
				// del.sync(zipName);

			},
			timeout: 10000
		})
	);
});

//清除临时中转文件tmpFile
gulp.task('cleanTmpFile1',['upload'],function() {
	console.log('start clear '+ webDist)
	return gulp.src([webDist],{read: false})
		.pipe(clean({force: true}));
});

gulp.task('build',['cleanTmpFile1'],function(){
	return '';
});

gulp.task('default', ['build']);